#!/usr/bin/env python
"""
调试AI API连接
"""
import requests
import json

def test_api_connection():
    """测试API连接"""
    
    # 测试基础连接
    test_urls = [
        "http://127.0.0.1:8000/api/ai/test/",
        "http://127.0.0.1:8000/api/ai/generate-medal/"
    ]
    
    for url in test_urls:
        print(f"\n{'='*50}")
        print(f"测试URL: {url}")
        print(f"{'='*50}")
        
        try:
            if 'test' in url:
                # GET请求测试
                response = requests.get(url, timeout=10)
                print(f"GET请求状态码: {response.status_code}")
            else:
                # POST请求测试
                test_data = {"prompt": "测试提示词"}
                response = requests.post(
                    url, 
                    json=test_data,
                    headers={'Content-Type': 'application/json'},
                    timeout=30
                )
                print(f"POST请求状态码: {response.status_code}")
            
            print(f"响应头: {dict(response.headers)}")
            print(f"响应内容: {response.text}")
            
            if response.status_code == 200:
                print("✅ 请求成功")
            else:
                print(f"❌ 请求失败: {response.status_code}")
                
        except requests.exceptions.ConnectionError:
            print("❌ 连接错误: 无法连接到服务器")
            print("请确保Django服务器正在运行: python manage.py runserver")
        except requests.exceptions.Timeout:
            print("❌ 请求超时")
        except Exception as e:
            print(f"❌ 其他错误: {str(e)}")

if __name__ == "__main__":
    test_api_connection()
